package crivia.ecp.carry.html.table;

import java.util.ArrayList;
import java.util.List;

import crivia.ecp.carry.html.DO;
import crivia.ecp.common.ECR;

public class Table extends DO {
	
	public Table(){}
	
	public Table(Table table){
		width = table.width;
		border = table.border;
		cellPadding = table.cellPadding;
		cellSpacing = table.cellSpacing;
		colGroup = table.colGroup;
		rows = table.rows;
	}

	@Override
	protected String name() {
		return "table";
	}

	@Override
	protected String innerHTML() {
		StringBuilder tStr = new StringBuilder();
		colGroup(tStr);
		
		rowSpan();
		
		for (TableRow row : rows){
			if (row.isShow())
				tStr.append(row);
		}
		return tStr.toString();
	}
	
	@Override
	public String toString() {
		if (border > -1)
			addAttr("border", border);
		if (cellSpacing > -1)
			addAttr("cellSpacing", cellSpacing);
		if (cellPadding > -1)
			addAttr("cellPadding", cellPadding);
		addAttr("width", width);
		if (null!=borderColor1px&&!"".equals(borderColor1px)){
			addStyle("border-collapse", "collapse");
			addStyle("border", "none");
		}
		return super.toString();
	}
	
	private int border = 0;
	public int getBorder() {
		return border;
	}
	public Table setBorder(int border) {
		this.border = border;
		return this;
	}
	
	private int cellSpacing = 0;
	public int getCellSpacing() {
		return cellSpacing;
	}
	public Table setCellSpacing(int cellSpacing) {
		this.cellSpacing = cellSpacing;
		return this;
	}
	
	private int cellPadding = 0;
	public int getCellPadding() {
		return cellPadding;
	}
	public Table setCellPadding(int cellPadding) {
		this.cellPadding = cellPadding;
		return this;
	}
	
	public Table setBCC(int border ,int cellSpacing ,int cellPadding){
		this.border = border;
		this.cellSpacing = cellSpacing;
		this.cellPadding = cellPadding;
		return this;
	}
	
	private String width = "100%";
	public String getWidth() {
		return width;
	}
	public Table setWidth(String width) {
		this.width = width;
		return this;
	}
	
	private boolean cutLongWord = false;
	public boolean isCutLongWord() {
		return cutLongWord;
	}
	public void setCutLongWord(boolean cutLongWord) {
		this.cutLongWord = cutLongWord;
	}
	
	private String borderColor1px = null;
	public String getBorderColor1px() {
		return borderColor1px;
	}
	public void setBorderColor1px(String borderColor1px) {
		this.borderColor1px = borderColor1px;
	}
	
	private List<TableColumn> colGroup = new ArrayList<TableColumn>();
	public TableColumn addColGroup(String width){
		TableColumn col = new TableColumn(this).setWidth(width); 
		colGroup.add(col);
		return col;
	}
	public Table addColGroup(int colSize){
		for (int k = 1; k <= colSize - 1; k++){
			colGroup.add(new TableColumn(this).setWidth(
					ECR.toNumber(width
							.replaceAll("[%]", "")
							.replaceAll("px", "")
							.trim()
					, int.class) / colSize + "%"
			));
		}
		colGroup.add(new TableColumn(this));
		return this;
	}
	public Table addColGroup(String... widths){
		if (null == widths){
			return this;
		}
		for (String width : widths){
			colGroup.add(new TableColumn(this).setWidth(width));
		}
		return this;
	}
	public List<TableColumn> getColGroup() {
		return colGroup;
	}
	public TableColumn getLastColGroup() {
		if (! colGroup.isEmpty()){
			return colGroup.get(colGroup.size() - 1);
		}
		return null;
	}
	public void clearColGroup(){
		colGroup = new ArrayList<TableColumn>();
	}
	
	private List<TableRow> rows = new ArrayList<TableRow>();
	public TableRow getRow(int rowIndex){
		if (rows.size() > rowIndex){
			return rows.get(rowIndex);
		}
		return null;
	}
	public List<TableRow> getRows() {
		return rows;
	}
	public Table setRows(List<TableRow> rows) {
		this.rows = rows;
		return this;
	}
	public Table addRows(List<TableRow> rows){
		this.rows.addAll(rows);
		return this;
	}
	public Table addRow(TableRow row){
		rows.add(row);
		return this;
	}
	public TableRow addRow(Object... cells){
		TableRow row = new TableRow(this);
		for (Object o : cells){
			row.addCell(o);
		}
		addRow(row);
		return row;
	}
	public int cellSize(){
		if (colGroup.size() > 0){
			return colGroup.size();
		}
		int tempNumber = 0;
		for (TableRow row : rows){
			if (! row.isShow()){
				continue;
			}
			int n = row.cellSize();
			if (n > tempNumber){
				tempNumber = n;
			}
		}
		return tempNumber;
	}
	
	protected void colGroup(StringBuilder tStr){
		if (colGroup.size() > 0) {
			if (cellSize() > colGroup.size()){
				throw Exception_CellsOver("Table Cell Size Is More Then Column Group Size.");
			}
			tStr.append("<colGroup>");
			for (TableColumn col : colGroup){
				tStr.append(col);
			}
		}
	}
	
	protected void rowSpan(){
		int rowSize = 0;
		for (TableRow row : rows){
			if (row.isShow()){
				rowSize ++;
			}
		}
		for (TableRow row : rows){
			if (! isShow()){
				continue;
			}
			for (TDCell cell : row.getCells()){
				if (cell.getRowSpan() > rowSize){
					cell.setRowSpan(rowSize);
				}
			}
			rowSize --;
		}
	}

}
